#include "database.h"

Database::Database()
{
    dbase = QSqlDatabase::addDatabase("QSQLITE");
    dbase.setHostName("localhost");
    dbase.setDatabaseName("1.db");
    if (!dbase.open())
    {
        QMessageBox msgBox;
        msgBox.setText("Error: Unable to establish a database connection");
        msgBox.exec();
    }
}

Database::~Database()
{
    QSqlDatabase::database("QSQLITE").close();
    QSqlDatabase::removeDatabase("QSQLITE");
}

QString Database::Ask()
{
    QString html;
    QSqlQuery asking;
    asking.exec("SELECT * FROM dict LIMIT 3");
    while (asking.next())
    {
        int id = asking.value(1).toInt();
        QString kanji = asking.value(2).toString();
        QString kana = asking.value(3).toString();
        QString znaczenie = asking.value(4).toString();
        html = html + " " + id + " " + kanji + " " + kana + " " + znaczenie;
    }
    return html;
}

